﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace ExportChangesets
{
    class Program
    {
        private static int startChangesetNumber = 0;
        private static int endChangesetNumber = 0;

        static void Main(string[] args)
        {
            Timer.GetElapsedTime(() =>
                {
                    //init logger
                    Logger logger = new Logger();

                    //check for command line args
                    if (!HasCommandLineArgs(args))
                    {
                        //check user inputs
                        GetUserInputChangesets();

                        //export
                        Export();
                    }
                });  
        }

        #region Private Methods
        private static void GetUserInputChangesets()
        {
            string firstUserInput = string.Empty;
            do
            {
                Console.WriteLine("First changeset number :");
                firstUserInput = Console.ReadLine();
            }
            while (!ValidInput(firstUserInput, out startChangesetNumber));

            string secondUserInput = string.Empty;
            do
            {
                Console.WriteLine("Second changeset number :");
                secondUserInput = Console.ReadLine();
            }
            while (!ValidInput(secondUserInput, out endChangesetNumber));
        }

        private static void Export()
        {
            Console.WriteLine("===============================================");
            Console.WriteLine(System.Environment.NewLine);
            Logger.WriteEntry("--> Starting Export...");

            int currentChangeset = startChangesetNumber;
            while (currentChangeset <= endChangesetNumber)
            {
                try
                {
                    Process process = new Process();
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.RedirectStandardOutput = true;
                    //process.StartInfo.FileName = "cmd.exe";
                    //process.StartInfo.Arguments = "/c date /t";
                    process.StartInfo.FileName = "tfpt.exe";
                    process.StartInfo.Arguments = string.Format(" getcs /changeset:{0} /overwrite",currentChangeset);
                    process.Start();
                    string output = process.StandardOutput.ReadToEnd();
                    process.WaitForExit();
                    Console.WriteLine(output);
                    Logger.WriteEntry("------------------------------");
                    Logger.WriteEntry(output);
                    Logger.WriteEntry("------------------------------");
                    Logger.WriteEntry("--");                    
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error when exporting changeset {0} : {1}",currentChangeset,e.Message);
                    Logger.WriteEntry(string.Format("Error when exporting changeset {0} : {1}", currentChangeset, e.Message));
                    break;
                }
                currentChangeset++;
            }

            Logger.WriteEntry("--> Export finished..");
            Console.WriteLine("===============================================");
        }

        private static bool ValidInput(string input,out int param)
        {
            param = 0;

            if (string.IsNullOrWhiteSpace(input)) return false;

            if (startChangesetNumber == 0)
            {
                return int.TryParse(input, out param);
            }
            else
            {
                if (int.TryParse(input, out param) && param >= startChangesetNumber)
                {
                    return true;
                }
                else
                {
                    param = 0;
                    return false;
                }
            }
        }

        private static bool HasCommandLineArgs(string[] args)
        {
            if (args.Count() != 2) return false;

            return ValidInput(args[0].ToString(), out startChangesetNumber) &&
                   ValidInput(args[1].ToString(), out startChangesetNumber);
        }
        #endregion
    }
}
